package dtm

import (
	"gitee.com/xuyiping_admin/pkg/xerr"
	"kpt-dtm/model"
	"time"
)

func (s *StoreEntry) GetFormulation(recipeId int) (formulation *model.Formulation, err error) {
	if err = s.DB.Table(new(model.Formulation).TableName()).
		Where("recipe_id = ?", recipeId).First(&formulation).Error; err != nil {
		return nil, err
	}
	return formulation, nil
}

func (s *StoreEntry) GetFormulationByIng(recipeId, ingId int) (formulation *model.Formulation, err error) {
	if err = s.DB.Table(new(model.Formulation).TableName()).
		Where("recipe_id = ?", recipeId).Where("ingredient_id = ?", ingId).First(&formulation).Error; err != nil {
		return nil, err
	}
	return formulation, nil
}

func (s *StoreEntry) CreateFormulation(formulationList []*model.Formulation) error {
	if err := s.DB.Table(new(model.Formulation).TableName()).Create(&formulationList).Error; err != nil {
		return xerr.WithStack(err)
	}
	return nil
}

func (s *StoreEntry) CreatedDryMatter(dryMatterList []*model.DryMatter) error {
	if err := s.DB.Table(new(model.DryMatter).TableName()).Create(&dryMatterList).Error; err != nil {
		return xerr.WithStack(err)
	}
	return nil
}

func (s *StoreEntry) CreateMixing(mixDataList []*model.MixData) error {
	if err := s.DB.Table(new(model.MixData).TableName()).Create(&mixDataList).Error; err != nil {
		return xerr.WithStack(err)
	}
	return nil
}

func (s *StoreEntry) CreateSpread(spreadList []*model.Spread) error {
	if err := s.DB.Table(new(model.Spread).TableName()).Create(&spreadList).Error; err != nil {
		return xerr.WithStack(err)
	}
	return nil
}

func (s *StoreEntry) CreateSurplus(surplusList []*model.Surplus) error {
	if err := s.DB.Table(new(model.Surplus).TableName()).Create(&surplusList).Error; err != nil {
		return xerr.WithStack(err)
	}
	return nil
}

func (s *StoreEntry) IsCheckFormulation(pastureId int64, recipeId, ingredientId string) bool {
	var count int64
	if err := s.DB.Model(new(model.Formulation)).
		Where("pasture_id = ?", pastureId).
		Where("recipe_id = ?", recipeId).
		Where("ingredient_id = ?", ingredientId).
		Count(&count).Error; err != nil {
		return false
	}
	// 存在则跳过
	if count > 0 {
		return true
	}
	return false
}

func (s *StoreEntry) IsCheckDryMatter(pastureId int64, elabLoadId int) bool {
	var count int64
	if err := s.DB.Table(new(model.DryMatter).TableName()).
		Where("pasture_id = ?", pastureId).
		Where("elabLoad_id = ?", elabLoadId).
		Count(&count).Error; err != nil {
		return false
	}
	// 存在则跳过
	if count > 0 {
		return true
	}
	return false
}

func (s *StoreEntry) IsCheckMixing(pastureId int64, loadDate string, recipeId, enableLoadId int) bool {
	var count int64
	if err := s.DB.Table(new(model.MixData).TableName()).
		Where("pasture_id = ?", pastureId).
		Where("load_date = ?", loadDate).
		Where("recipe_id = ?", recipeId).
		Where("elabLoad_id = ?", enableLoadId).
		Count(&count).Error; err != nil {
		return false
	}
	if count > 0 {
		return true
	}
	return false
}

func (s *StoreEntry) IsCheckSpread(pastureId int64, spreadId int) bool {
	var count int64
	if err := s.DB.Table(new(model.Spread).TableName()).
		Where("pasture_id = ?", pastureId).
		Where("spread_id = ?", spreadId).
		Count(&count).Error; err != nil {
		return false
	}
	// 存在则跳过
	if count > 0 {
		return true
	}
	return false
}

func (s *StoreEntry) IsCheckSurplus(pastureId int64, penId int, refusedDate string) bool {
	var count int64
	if err := s.DB.Table(new(model.Surplus).TableName()).
		Where("pasture_id = ?", pastureId).
		Where("pen_id = ?", penId).
		Where("refused_date = ?", refusedDate).
		Count(&count).Error; err != nil {
		return false
	}
	// 存在则跳过
	if count > 0 {
		return true
	}
	return false
}

func (s *StoreEntry) CreateLoop(pastureId int64, name string) error {
	if err := s.DB.Table(new(model.Loop).TableName()).
		Create(model.NewLoop(pastureId, name)).Error; err != nil {
		return xerr.WithStack(err)
	}
	return nil
}

func (s *StoreEntry) GetLoopList() []*model.Loop {
	nowTime := time.Now()
	midnight := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
	createdTime := midnight.Unix()
	loopList := make([]*model.Loop, 0)
	if err := s.DB.Table(new(model.Loop).TableName()).
		Where("created_at >= ?", createdTime).
		Find(&loopList).Error; err != nil {
	}
	return loopList
}

func (s *StoreEntry) CreateErrorLog(pastureId int64, name, message string) {
	data := model.NewErrorLog(pastureId, name, message)
	s.DB.Table(new(model.ErrorLog).TableName()).Create(data)
}
